#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 28 09:50:24 2019

@author: ubuntu
"""


# predict target cross
import numpy as np
import pickle as pkl
import pandas as pd
import os


pwd = os.getcwd()
[flow,relate_cross,crossName,target_cross,pred_cross,similar_cross,roadNet,submitList,data_tr]=pkl.load(open(pwd+"/data.pkl",'rb'))



# --------------------------------- acnn_model -----------------------------------------

flow = pkl.load(open(pwd+"/result/acnn_result.pkl",'rb'))           
 
# 1 acnn_model: pred_cross ----average transfer to---> target_cross
for k in range(len(submitList)):
    time_ = submitList.loc[k,'timeStamp']
    cross = submitList.loc[k,'crossroadID']
    pred = 0.0
    cnt = 0
    for rel_cross in relate_cross[cross]:
        pred = pred + flow.loc[time_, rel_cross]
        cnt = cnt + 1
    pred = pred / cnt
    submitList.loc[k,'value'] = pred
submitList.to_csv(open(pwd+"/result/submit_acnn_mean.csv",'w'),index=False,\
                  columns=['date','crossroadID','timeBegin','value'],encoding='utf-8')


# 2 acnn_model: pred_cross ----weighted transfer to---> target_cross
for k in range(len(submitList)):
    time_ = submitList.loc[k,'timeStamp']
    cross = submitList.loc[k,'crossroadID']
    pred = 0.0
    cnt = 0
    for rel_cross in relate_cross[cross]:
        if cross in roadNet and rel_cross in roadNet[cross]:
            pred = pred + 2*flow.loc[time_, rel_cross]
            cnt = cnt + 2
        else:
            pred = pred + flow.loc[time_, rel_cross]
        cnt = cnt + 1
    pred = pred / cnt
    submitList.loc[k,'value'] = pred
submitList.to_csv(open(pwd+"/result/submit_acnn_wht.csv",'w'),index=False,\
                  columns=['date','crossroadID','timeBegin','value'],encoding='utf-8')
 




# --------------------------------- rw_model -----------------------------------------

flow = pkl.load(open(pwd+"/result/rw_result.pkl",'rb'))

# 3 rw_model: pred_cross ----average transfer to---> target_cross
for k in range(len(submitList)):
    time_ = submitList.loc[k,'timeStamp']
    cross = submitList.loc[k,'crossroadID']
    pred = 0.0
    cnt = 0
    for rel_cross in relate_cross[cross]:
        pred = pred + flow.loc[time_, rel_cross]
        cnt = cnt + 1
    pred = pred / cnt
    submitList.loc[k,'value'] = pred
submitList.to_csv(open(pwd+"/result/submit_rw_mean.csv",'w'),index=False,\
                  encoding='utf-8',columns=['date','crossroadID','timeBegin','value'])


# 4 rw_model: pred_cross ----weighted transfer to---> target_cross
for k in range(len(submitList)):
    time_ = submitList.loc[k,'timeStamp']
    cross = submitList.loc[k,'crossroadID']
    pred = 0.0
    cnt = 0
    for rel_cross in relate_cross[cross]:
        if cross in roadNet and rel_cross in roadNet[cross]:
            pred = pred + 2*flow.loc[time_, rel_cross]
            cnt = cnt + 2
        else:
            pred = pred + flow.loc[time_, rel_cross]
        cnt = cnt + 1
    pred = pred / cnt
    submitList.loc[k,'value'] = pred
submitList.to_csv(open(pwd+"/result/submit_rw_wht.csv",'w'),index=False,\
                  columns=['date','crossroadID','timeBegin','value'],encoding='utf-8')







